from datetime import datetime

from django.contrib.auth.models import AbstractUser
from django.db import models


# Create your models here.
class BaseModel(models.Model):
    """创建一个基础model类，用来继承一些公用的字段"""
    # 注意：此处填引用而非调用，Django会在适当时机调用，而非解释器运行时间
    add_time = models.DateTimeField("添加时间", default=datetime.now)

    class Meta:
        abstract = True  # 不会生成表


class UserProfile(AbstractUser):
    """用户表进行扩展"""
    GENDER_CHOICES = (
        ("male", "男"),
        ("female", "女"),
    )
    nick_name = models.CharField("昵称", max_length=50, default="")
    birthday = models.DateField("生日", blank=True, null=True)
    gender = models.CharField("性别", max_length=6, choices=GENDER_CHOICES)
    address = models.CharField("地址", max_length=200, default="")
    mobile = models.CharField("手机号", max_length=11)
    image = models.ImageField("头像", upload_to="head_image/%Y/%m", default="default.jpg")

    class Meta:
        verbose_name = "用户信息"
        verbose_name_plural = verbose_name

    def __str__(self):
        if self.nick_name:
            return self.nick_name
        return self.username
